package com.ittest.web.controller.system;

import com.github.pagehelper.PageInfo;
import com.ittest.domain.system.Permission;
import com.ittest.domain.system.Role;
import com.ittest.service.system.PermissionService;
import com.ittest.service.system.impl.PermissionServiceImpl;
import com.ittest.utils.BeanUtil;
import com.ittest.web.controller.BaseServlet;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 *   角色控制层
 */
@WebServlet("/system/role/*")
public class RoleServlet extends BaseServlet {
    /**
     * 查询所有
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取模糊查询参数
        String nameStr = request.getParameter("nameStr");

        // 默认设置，当前页1
        int page = 1;
        // 默认设置，每页条数10
        int size = 5;
        // 判断页面是否获取值，有值，按页面参数进行分页
        // 当前页面
        if (StringUtils.isNotBlank(request.getParameter("page"))){
            page = Integer.parseInt(request.getParameter("page"));
        }
        // 每页显示数据条数
        if (StringUtils.isNotBlank(request.getParameter("size"))){
            size = Integer.parseInt(request.getParameter("size"));
        }

        // 调用service，查询数据
        PageInfo all = roleService.findByName(page,size,nameStr);
        // 查询结果存入作用域
        request.setAttribute("nameStr",nameStr);
        request.setAttribute("page",all);
        request.setAttribute("size",size);
        // 跳转  /pages/system/syslog/syslog-list.jsp页面
        request.getRequestDispatcher("/pages/system/role/role-list.jsp").forward(request,response);
    }

    /**
     *  添加保存操作
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void save(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        //获取页面数据
        Role role= BeanUtil.fillBean(request,Role.class);
        roleService.add(role);
        request.getRequestDispatcher("/system/role/list").forward(request,response);
    }

    /**
     * 批量删除功能
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        // 获取前台页面arr（批量删除id数据）
        String arr = request.getParameter("arr");
        // 调用业务层实现批删除操作
        roleService.deleteByIds(arr);
        // 删除完成，跳回list页面
        response.sendRedirect(request.getContextPath()+"/system/role/list");

    }
    /**
     * 跳转更改权限界面
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void toChange(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        PermissionService permissionService = new PermissionServiceImpl();
        // 查询所有的权限
        List<Permission> permissionList = permissionService.findAll(1, 10);
        request.setAttribute("permissionList",permissionList);
        // 获取当前的角色id
        String id = request.getParameter("id");
        request.setAttribute("roleId",id);
        // 调用方法查询角色对应的权限
        List<Permission> list = roleService.findPermissionByRole(id);
        request.setAttribute("permissionList",list);
        // 页面跳转
        request.getRequestDispatcher("/pages/system/role/role-permission-add.jsp").forward(request,response);
    }
    /**
     * 给角色添加权限
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void addPermissionToRole(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        // 获取角色的id
        String roleId = request.getParameter("roleId");
        // 获取对应权限的id
        String[] permissionIds = request.getParameterValues("ids");
        // 调用业务层方法
        roleService.addPermissionToRole(roleId,permissionIds);
        // 页面跳转
        request.getRequestDispatcher("/system/role/list").forward(request,response);
    }
}
